<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户名称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入用户名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
         
          
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

     
        <el-tabs v-model="tabActiveName"  @tab-click="handleTabClick">
          <el-tab-pane label="全部" name="all"></el-tab-pane>
          <el-tab-pane label="未认证" name="1"></el-tab-pane>
          <el-tab-pane label="已认证" name="2"></el-tab-pane>
          <el-tab-pane label="未通过" name="3"></el-tab-pane>
        </el-tabs>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="所属区域" align="center" prop="areaName" >
            <template slot-scope="scope">
              {{scope.row.areaName?scope.row.areaName.replaceAll("#",""):""}}
            </template>
          </el-table-column>
          <el-table-column label="用户编号" align="center" key="userId" prop="userId" v-if="columns[0].visible" />
          <!-- <el-table-column label="用户头像" align="center" key="avatar" prop="avatar" >
            <template slot-scope="scope">
              <el-image  v-if="scope.row.avatar"
                style="width: 40px; height: 40px;border-radius: 50%;"
                :src="scope.row.avatar" 
                >
              </el-image>
            </template>
          </el-table-column> -->
          <el-table-column label="账号" align="center" key="userName" prop="userName" v-if="columns[1].visible" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <el-button
                  type="text"
                  @click="handleShow(scope.row)"
                >{{scope.row.userName}}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" v-if="columns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="用户性别" align="center" prop="sex" :formatter="sexFormat" />
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" v-if="columns[4].visible" width="120" />
          <!-- <el-table-column label="所属公司" align="center" prop="company.name" :show-overflow-tooltip="true" /> -->
          <el-table-column label="身份证号" align="center" prop="idcard" >
            <template slot-scope="scope">
              {{scope.row.idcard}}
            </template>  
          </el-table-column>        
          <el-table-column label="身份证正面" align="center" prop="cardZheng" >
            <template slot-scope="scope">
              <el-image  v-if="scope.row.cardZheng"
                style="width: 60px; height: auto"
                :src="scope.row.cardZheng" 
                :preview-src-list="[scope.row.cardZheng]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="身份证反面" align="center" prop="cardFan" >
            <template slot-scope="scope">
              <el-image v-if="scope.row.cardFan"
                style="width: 60px; height: auto"
                :src="scope.row.cardFan" 
                :preview-src-list="[scope.row.cardFan]">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="服务范围" align="center" prop="tags" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.tags?scope.row.tags.split(";").map(a=>a.split("=")[1]).filter(a=>a).join():'' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="注册时间" align="center" prop="createTime" v-if="columns[6].visible" width="170">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="认证状态" align="center" prop="authStatus" >
            <template slot-scope="scope">
              <span>{{scope.row.authStatus == 1 ? '未认证' : scope.row.authStatus == 2 ? '已认证' : '未通过' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="120"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-search"
                @click="handleShow(scope.row)"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-coordinate"
                @click="handleApprove(scope.row)"
              >审核</el-button>            
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
<!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" :disabled="true">
        <el-row>       
          <el-col :span="12">
            <el-form-item  label="账号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idcard">
              <el-input v-model="form.idcard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="真实姓名" prop="realName">
              <el-input v-model="form.realName" placeholder="真实姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="身份证正面" prop="cardZheng">
              <imageUpload v-model="form.cardZheng"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证反面" prop="cardFan">
              <imageUpload v-model="form.cardFan"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>          
          <el-col :span="24">
            <el-form-item label="服务范围">
              <el-input v-model="form.tagshow" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-tabs >
          <el-tab-pane label="技能证书">
            <el-row>
              <el-col :span="24">
                <el-tabs tab-position="left" style="height: 150px;">
                  <el-tab-pane v-for="item in form.certList" v-bind:key="item" :label="item.name">
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="证书照片" prop="fileUrl">
                          <imageUpload v-model="item.fileUrl" style="height:60px;"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="服务范围" prop="tags">
                          <el-input v-model="item.tags" type="textarea" placeholder=""></el-input>
                        </el-form-item>
                      </el-col>
                      
                    </el-row>
                    
                  </el-tab-pane>                          
                </el-tabs>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs> -->
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitPass" v-if="approved">通 过</el-button>
        <el-button type="warning" @click="submitNotPass" v-if="approved">驳 回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
   
  </div>
</template>

<script>
import { listProxyApproveUser, getUser, approveUser } from "@/api/system/user";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import ImageUpload from '@/components/ImageUpload';
import { treeArea } from "@/api/system/area";

export default {
  name: "User",
  components: { Treeselect,ImageUpload },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      orderloading: true,
      orderShow: false,
      approved: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      orderTotal: 0,
      // 用户表格数据
      userList: null,
      orderList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      proxyOptions: undefined,
      areaOptions:[],
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData"
      },
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        proxyId: undefined
      },
      // 查询参数
      queryOrderParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true }
      ],
      
    };
  },
  // watch: {
  //   // 根据名称筛选部门树
  //   deptName(val) {
  //     this.$refs.tree.filter(val);
  //   }
  // },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
    this.getDicts("tb_order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getAreaTree();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listProxyApproveUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getAreaTree() {
      treeArea().then(response => {
        this.areaOptions = response.data;
      });
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 用户性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    handleTabClick(tab){
      if (tab.name != 'all') {
        this.queryParams.authStatus = tab.name;
      }else{
        this.queryParams.authStatus = '';
      }
      this.getList();

    },
    // 取消按钮
    cancel() {
      this.approved = false;
      this.orderShow = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
   
   /** 新增按钮操作 */
    handleShow(row) {
      this.approved = false;
      this.reset();
      getUser(row.userId).then(response => {
        this.form = response.data;
        if (this.form.tags) {
          this.form.tagshow = this.form.tags.split(";").map(a=>a.split("=")[1]).filter(a=>a).join();
        }
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "查看";
        
      });
    },
   /** 审核按钮操作 */
    handleApprove(row) {
     
      this.reset();
      getUser(row.userId).then(response => {
        this.approved = true;
        this.form = response.data;
        if (this.form.tags) {
          this.form.tagshow = this.form.tags.split(";").map(a=>a.split("=")[1]).filter(a=>a).join();
        }
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "查看";
        
      });
    },
  
    
    /** 提交按钮 */
    submitPass: function() {
      this.form.authStatus = 2;
      approveUser({
        userId: this.form.userId,
        authStatus: this.form.authStatus
      }).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
     /** 提交按钮 */
    submitNotPass: function(){
      this.form.authStatus = 3;
      approveUser({
        userId: this.form.userId,
        authStatus: this.form.authStatus
      }).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
    
  }
};
</script>